Add Cosmos DB dedicated gateway bypass cache and shard key support#44846
Add Cosmos DB dedicated gateway bypass cache and shard key support#44846
Conversation
- Added HTTP header constants for bypass cache and shard key - Added validation function for shard key (alphanumeric + hyphen only) - Updated GetHeaders to send bypass and shard key headers - Added parameters to sync container methods: read_item, read_all_items, query_items - Added parameters to async container methods with same functionality - Updated docstrings for new parameters Co-authored-by: simorenoh <30335873+simorenoh@users.noreply.github.com>
…eway_shard_key - Fixed bypass cache header to be sent when value is False - Added unit tests for bypass_integrated_cache with True/False/None values - Added unit tests for dedicated_gateway_shard_key validation - Added test for invalid shard key characters - Added test for combined bypass cache and shard key usage in queries - All validation and header generation tests passing Co-authored-by: simorenoh <30335873+simorenoh@users.noreply.github.com>
- Updated all query_items overloads in async container to include new parameters - Ensured consistency across all method signatures - All sync and async methods now support bypass_integrated_cache and dedicated_gateway_shard_key Co-authored-by: simorenoh <30335873+simorenoh@users.noreply.github.com>
- Improved boolean-to-string conversion efficiency in GetHeaders - Updated tests to use pytest.raises() instead of try-except blocks - Applied modern Python testing practices for cleaner exception testing Co-authored-by: simorenoh <30335873+simorenoh@users.noreply.github.com>
- Fixed inconsistent null checking for max_integrated_cache_staleness_in_ms - Changed truthiness check to explicit 'is not None' to handle 0 values correctly - Added missing docstrings for bypass_integrated_cache and dedicated_gateway_shard_key in async read_item - Ensures consistency between sync and async documentation Co-authored-by: simorenoh <30335873+simorenoh@users.noreply.github.com>
- Fixed string quote consistency (use double quotes throughout) - Fixed parameter indentation alignment in tests - All code now follows consistent formatting standards Co-authored-by: simorenoh <30335873+simorenoh@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Adds missing Cosmos DB dedicated gateway request controls to the Python SDK to match Java capabilities, enabling per-request integrated cache bypass and shard-based routing via new HTTP headers.
Changes:
- Introduces dedicated gateway headers for cache bypass and shard key routing and wires them through request option handling.
- Extends sync + async container APIs (
read_item,read_all_items,query_items) withbypass_integrated_cacheanddedicated_gateway_shard_keysupport (including shard key validation). - Adds/updates header-focused unit tests for the new headers (sync).
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| sdk/cosmos/azure-cosmos/tests/test_headers.py | Adds tests validating dedicated gateway bypass-cache + shard-key headers, and modernizes one ValueError assertion. |
| sdk/cosmos/azure-cosmos/azure/cosmos/http_constants.py | Adds constants for the two new dedicated gateway headers. |
| sdk/cosmos/azure-cosmos/azure/cosmos/container.py | Adds new request options for bypass-cache + shard-key to sync read/query flows. |
| sdk/cosmos/azure-cosmos/azure/cosmos/aio/_container.py | Adds new request options for bypass-cache + shard-key to async read/query flows. |
| sdk/cosmos/azure-cosmos/azure/cosmos/_base.py | Emits the new headers in GetHeaders and adds shard key validation helper. |
| session_token: Optional[str] = None, | ||
| initial_headers: Optional[dict[str, str]] = None, | ||
| max_integrated_cache_staleness_in_ms: Optional[int] = None, | ||
| bypass_integrated_cache: Optional[bool] = None, | ||
| dedicated_gateway_shard_key: Optional[str] = None, |
There was a problem hiding this comment.
aio.ContainerProxy.read_all_items adds new parameters bypass_integrated_cache and dedicated_gateway_shard_key, but the method docstring below the signature does not document these keywords. Please update the docstring so async users discover the new options via help()/generated docs.
There was a problem hiding this comment.
Added missing docstrings for bypass_integrated_cache and dedicated_gateway_shard_key parameters in async read_all_items method. Also addressed other review feedback:
- Fixed
maxIntegratedCacheStalenessnull check to support value 0 - Added type validation for shard key
- Updated runtime
query_itemsdocstring - Added comprehensive tests
Commit: cf89cc9
| if utils.valid_key_value_exist(kwargs, "bypass_integrated_cache"): | ||
| bypass_integrated_cache = kwargs.pop("bypass_integrated_cache") | ||
| feed_options["bypassIntegratedCache"] = bypass_integrated_cache | ||
| if utils.valid_key_value_exist(kwargs, "dedicated_gateway_shard_key"): | ||
| dedicated_gateway_shard_key = kwargs.pop("dedicated_gateway_shard_key") | ||
| validate_shard_key_value(dedicated_gateway_shard_key) | ||
| feed_options["dedicatedGatewayShardKey"] = dedicated_gateway_shard_key |
There was a problem hiding this comment.
Async dedicated gateway headers are now supported (bypass_integrated_cache / dedicated_gateway_shard_key), but there are no tests covering that the async query_items path propagates these options into request headers. Adding coverage in the async headers tests would help ensure parity with the sync behavior.
There was a problem hiding this comment.
Added async query_items test in test_headers_async.py that verifies bypass_integrated_cache and dedicated_gateway_shard_key headers are correctly propagated. The test uses async iteration and follows the same pattern as sync tests. Commit: cf89cc9
| if options.get("maxIntegratedCacheStaleness"): | ||
| headers[http_constants.HttpHeaders.DedicatedGatewayCacheStaleness] = options["maxIntegratedCacheStaleness"] | ||
|
|
||
| if "bypassIntegratedCache" in options: | ||
| headers[http_constants.HttpHeaders.DedicatedGatewayBypassCache] = "true" if options["bypassIntegratedCache"] else "false" |
There was a problem hiding this comment.
GetHeaders only sets x-ms-dedicatedgateway-max-age when maxIntegratedCacheStaleness is truthy, so a valid value of 0 (allowed by validate_cache_staleness_value) will silently omit the header. Consider checking for key presence / is not None instead so 0 is transmitted correctly.
There was a problem hiding this comment.
Fixed to use key presence check (if "maxIntegratedCacheStaleness" in options) instead of truthy check. This ensures value 0 is correctly transmitted as a header. Commit: cf89cc9
| :keyword int max_integrated_cache_staleness_in_ms: The max cache staleness for the integrated cache in | ||
| milliseconds. For accounts configured to use the integrated cache, using Session or Eventual consistency, | ||
| responses are guaranteed to be no staler than this value. | ||
| :keyword bool bypass_integrated_cache: If set to True, the read will be served by the backend and won't be | ||
| cached in the dedicated gateway. If set to False or not provided, the integrated cache will be used if | ||
| configured. This option only applies to accounts configured with dedicated gateway. | ||
| :keyword str dedicated_gateway_shard_key: The shard key to use for the dedicated gateway request. Specifying | ||
| the shard key will help route the request to an instance that has cached data for this shard or bypass | ||
| the SQLx cache if the correlated instance is down. If not specified, the request will fall back to | ||
| randomly selecting an instance. This option only applies to accounts configured with dedicated gateway. |
There was a problem hiding this comment.
The bypass_integrated_cache / dedicated_gateway_shard_key parameter docs were added to the @overload docstring, but the runtime implementation of query_items is the def query_items(self, *args, **kwargs) definition later in the file (overload stubs are overwritten). To ensure help()/Sphinx show these new options, the implementation docstring should also document them (or the docs should live only on the implementation).
| :keyword int max_integrated_cache_staleness_in_ms: The max cache staleness for the integrated cache in | |
| milliseconds. For accounts configured to use the integrated cache, using Session or Eventual consistency, | |
| responses are guaranteed to be no staler than this value. | |
| :keyword bool bypass_integrated_cache: If set to True, the read will be served by the backend and won't be | |
| cached in the dedicated gateway. If set to False or not provided, the integrated cache will be used if | |
| configured. This option only applies to accounts configured with dedicated gateway. | |
| :keyword str dedicated_gateway_shard_key: The shard key to use for the dedicated gateway request. Specifying | |
| the shard key will help route the request to an instance that has cached data for this shard or bypass | |
| the SQLx cache if the correlated instance is down. If not specified, the request will fall back to | |
| randomly selecting an instance. This option only applies to accounts configured with dedicated gateway. |
| if bypass_integrated_cache is not None: | ||
| feed_options["bypassIntegratedCache"] = bypass_integrated_cache | ||
| if dedicated_gateway_shard_key is not None: | ||
| validate_shard_key_value(dedicated_gateway_shard_key) | ||
| feed_options["dedicatedGatewayShardKey"] = dedicated_gateway_shard_key |
There was a problem hiding this comment.
read_all_items now accepts bypass_integrated_cache / dedicated_gateway_shard_key, but there is no test asserting these options are translated into the expected dedicated gateway headers for the ReadItems path. Adding a unit test similar to the existing read_item header tests would help prevent regressions.
There was a problem hiding this comment.
Added test_read_all_items_with_bypass_and_shard_key() in test_headers.py that verifies both headers are correctly set for the ReadItems path. The test follows the same mocking pattern as existing read_item tests. Commit: cf89cc9
API Change CheckAPIView identified API level changes in this PR and created the following API reviews |
- Add missing docstrings for bypass_integrated_cache and dedicated_gateway_shard_key in async read_all_items - Fix maxIntegratedCacheStaleness to use key presence check instead of truthy check (supports value 0) - Add type validation to validate_shard_key_value to handle non-string types gracefully - Add bypass_integrated_cache and dedicated_gateway_shard_key docs to query_items implementation docstring - Add test for read_all_items with bypass and shard key headers - Add async test for query_items with bypass and shard key headers Agent-Logs-Url: https://github.com/Azure/azure-sdk-for-python/sessions/3867bf07-f0b6-427f-bc2d-4f2529ba716d Co-authored-by: simorenoh <30335873+simorenoh@users.noreply.github.com>
|
Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
Implements two dedicated gateway features missing from Python SDK, bringing parity with Java SDK: cache bypass control and shard-based routing.
Changes
HTTP Headers
x-ms-dedicatedgateway-bypass-cache- control cache usage per requestx-ms-dedicatedgateway-shard-key- route to specific gateway instancesAPI Surface
Added optional parameters to
read_item(),read_all_items(), andquery_items()(sync + async):bypass_integrated_cache: Optional[bool]- whenTrue, bypasses cache and reads from backenddedicated_gateway_shard_key: Optional[str]- routes request to instance with cached shard dataValidation
is not NonethroughoutUsage
References
Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
scanning-api.github.com/home/REDACTED/work/_temp/ghcca-node/node/bin/node /home/REDACTED/work/_temp/ghcca-node/node/bin/node --enable-source-maps /home/REDACTED/work/_temp/copilot-developer-action-main/dist/index.js(dns block)If you need me to access, download, or install something from one of these locations, you can either:
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.